home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / scope / 176-200 / scopedisk180 / arexxtutorial / function_host / rh_demo.c < prev    next >
C/C++ Source or Header  |  1995-03-19  |  7KB  |  320 lines

  1. /*  ARexx_Host module Demo   */
  2.  
  3.  
  4. /*        Copyright © 1989 by Donald T. Meyer, Stormgate Software
  5.  *        All Rights Reserved
  6.  *
  7.  *        This source code may be compiled and used in any software
  8.  *        product.
  9.  *        No portion of this source code is to be
  10.  *        re-distributed or sold for profit without the written
  11.  *        permission of the author, Donald T. Meyer.
  12.  *
  13.  *        Stormgate Software
  14.  *        PO Box 383
  15.  *        St. Peters, MO  63376
  16.  *
  17.  *        E-Mail can be sent to via the following:
  18.  *        BIX:    donmeyer        (almost daily)
  19.  *        GEnie:    D.MEYER            (weekly)
  20.  *        PLINK:    Stormgate        (weekly)
  21.  */
  22.  
  23.  
  24.  
  25. #include <libraries/dos.h>
  26. #include <intuition/intuition.h>
  27.  
  28. #include <proto/intuition.h>
  29. #include <proto/exec.h>
  30. #include <proto/dos.h>
  31. #include <string.h>
  32. #include <stdio.h>
  33.  
  34.  
  35.  
  36. /*------------------------------------------------------------------*/
  37. /*                    External Function Declarations                    */
  38. /*------------------------------------------------------------------*/
  39.  
  40.  
  41. /*------------------------------------------------------------------*/
  42. /*                    Local Function Declarations                        */
  43. /*------------------------------------------------------------------*/
  44.  
  45. /* Declare all of the host function functions */
  46.  
  47. void func_rats( struct RexxMsg * );
  48. void func_dbeep( struct RexxMsg * );
  49. void func_bigword( struct RexxMsg * );
  50. void func_myerror( struct RexxMsg * );
  51. void func_filename( struct RexxMsg * );
  52.  
  53.  
  54.  
  55. /*------------------------------------------------------------------*/
  56. /*                    Variable Definitions                            */
  57. /*------------------------------------------------------------------*/
  58.  
  59. /* This is the stuff to let us become detached... */
  60.  
  61. long _stack = 4096;
  62. long _priority = 0;
  63. char *_procname = "Demo_ARexxHost";
  64.  
  65.  
  66.  
  67. char *hostname = "DEMO_FUNC_HOST";
  68. int hostpri = 5;
  69.  
  70. ULONG client_event_flags = NULL;
  71.  
  72. char *hello_string = "\x1B[33mDemo ARexx Function Host\x1B[31m\
  73. Version 1.2\n\
  74. Copyright \xA9 1989 by Don Meyer, Stormgate Software\n";
  75.  
  76. char *success_string = "Function Host installation successful!\n";
  77.  
  78. char *removal_string = "Removing existing demo function host!\n";
  79.  
  80. char *redundant_string =
  81.     "Oops, we already are running.  No action taken.\n";
  82.  
  83. char *noclone_string = "No existing demo host to remove!\n";
  84.  
  85.  
  86. char *console_def_string = "CON:30/30/500/60/ARexx Function Host";
  87.  
  88.  
  89.  
  90. struct RexxFunction func_table[] = {
  91.     { "rats", &func_rats, 0, FALSE },
  92.     { "dbeep", &func_dbeep, 0, FALSE },
  93.     { "bigword", &func_bigword, 1, FALSE },
  94.     { "myerror", &func_myerror, -1, FALSE },
  95.     { "filename", &func_filename, 1, FALSE },
  96.  
  97.     /* Mark end-of-table */
  98.     { NULL, NULL, 0, FALSE }
  99. };
  100.  
  101.  
  102.  
  103. /* These are needed only if these librarys will be opened and used. */
  104.  
  105. struct IntuitionBase *IntuitionBase = NULL;
  106. struct GfxBase *GfxBase = NULL;
  107.  
  108.  
  109.  
  110. /*------------------------------------------------------------------*/
  111. /*                    Functions                                        */
  112. /*------------------------------------------------------------------*/
  113.  
  114.  
  115.         /*****************************************
  116.          **                                        **
  117.          **            The Host Functions            **
  118.          **                                        **
  119.          ****************************************/
  120.  
  121.  
  122.  
  123. /* Note that in the "manual method" of returning an argstring, there
  124.  * is no checking for failure to allocate the argstring.  To be
  125.  * robust, this should be done.  The SetResultString helper function
  126.  * _does_ do this.  The "manual" method is shown more for illustration,
  127.  * as SetResultString() is probably the best way to return an
  128.  * argstring.  The notable exception would be when returning an
  129.  * argstring with embedded nulls.  SetResultString() will see the
  130.  * first null as the string terminator.
  131.  */
  132.  
  133.  
  134.  
  135. /* A function which returns nothing */
  136. /*
  137.  *    call dbeep()
  138.  */
  139.  
  140. void func_dbeep( struct RexxMsg *rexxmsg )
  141. {
  142.     DisplayBeep( NULL );
  143.  
  144.     /* We don't do anything to set a result, since we return nothing
  145.      * and there were no errors.
  146.      */
  147. }
  148.  
  149.  
  150.  
  151. /* A function which returns a string */
  152. /*
  153.  *        say rats()
  154.  */
  155.  
  156. void func_rats( struct RexxMsg *rexxmsg )
  157. {
  158.     SetResultString( rexxmsg, "Large Rodents!" );
  159.  
  160.  
  161. /* "manual" method -----v                                        */
  162. /*                                                                */
  163. /*    rexxmsg->rm_Result2 =                                        */
  164. /*        (ULONG)CreateArgstring( "Large Rodents!", (LONG)14 );    */
  165. /*                                                                */
  166. }
  167.  
  168.  
  169.  
  170. /* A function which takes one argument and returns a boolean */
  171. /*
  172.  *        say bigword( "Supercalifragilisticexpaladouchs" )
  173.  *
  174.  *  Okay, okay, YOU spell it correctly!  Sheesh.
  175.  */
  176.  
  177. void func_bigword( struct RexxMsg *rexxmsg )
  178. {
  179.     if(  LengthArgstring( (struct RexxArg *)ARG1(rexxmsg) ) > 15  )
  180.     {
  181.         SetResultString( rexxmsg, "1" );
  182.     }
  183.     else
  184.     {
  185.         SetResultString( rexxmsg, "0" );
  186.     }
  187. }
  188.  
  189.  
  190.  
  191. /* Returns a function error no matter what */
  192.  
  193. void func_myerror( struct RexxMsg *rexxmsg )
  194. {
  195.     SetResultString( rexxmsg, NULL );
  196.  
  197.  
  198. /* "manual" method -----v                                        */
  199. /*                                                                */
  200. /*    rexxmsg->rm_Result1 = RC_ERROR;                                */
  201. /*    rexxmsg->rm_Result2 = ERR10_012                                */
  202. }
  203.  
  204.  
  205.  
  206. /* Takes a fully qualified path and filename and returns just
  207.  * the filename.  This function is NOT optimized, just
  208.  * a quick-and-dirty example.  As a matter of fact, I am not
  209.  * even going to proffess it to be bug-free! :-)
  210.  *
  211.  *        say( "df0:tests/startrek" )
  212.  *
  213.  *                returns -->  "startrek"
  214.  */ 
  215.  
  216. void func_filename( struct RexxMsg *rexxmsg )
  217. {
  218.     char buf[256];
  219.     int i;
  220.  
  221.  
  222.     /* Copy to a buffer.  Not really neccessary, but this IS an
  223.      * example, so...
  224.      */
  225.     if(  LengthArgstring( (struct RexxArg *)ARG1(rexxmsg) ) > 255  )
  226.     {
  227.         /* Won't fit in our buffer, return an error. */
  228.         SetResultString( rexxmsg, NULL );
  229.         return;
  230.     }
  231.  
  232.  
  233.     strcpy( buf, ARG1(rexxmsg) );
  234.  
  235.     /* Scan for a "/" character */
  236.     for( i=strlen(buf)-1; i>=0; i-- )
  237.     {
  238.         if( buf[i] == '/' )
  239.         {
  240.             /* Found one! */
  241.             SetResultString( rexxmsg, &buf[i+1] );
  242.             return;
  243.         }
  244.     }
  245.  
  246.     /* Okay, no "/"s, then scan for a ":" character */
  247.     for( i=strlen(buf)-1; i>=0; i-- )
  248.     {
  249.         if( buf[i] == ':' )
  250.         {
  251.             /* Found one! */
  252.             SetResultString( rexxmsg, &buf[i+1] );
  253.             return;
  254.         }
  255.     }
  256.  
  257.     /* Hmmm.  Must _already_ be just the file name! */
  258.     SetResultString( rexxmsg, buf );
  259. }
  260.  
  261.  
  262.  
  263. /*::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
  264.  
  265.  
  266. /* In this demo this is merely a dummy function.  In other hosts
  267.  * this would handle any events caused by client-specific needs.
  268.  */
  269.  
  270. void client_event_handler( ULONG flags )
  271. {
  272. }
  273.  
  274.  
  275.  
  276. /* These two functions will be called by the startup and cleanup.
  277.  * Typical use is to open librarys, etc.
  278.  */
  279.  
  280.  
  281. /* This should return a pointer to an error message string to
  282.  * indicate failure
  283.  */
  284.  
  285. char *client_init( void )
  286. {
  287.     /*   Intuition   */
  288.     if(  ! ( IntuitionBase = (struct IntuitionBase *)
  289.         OpenLibrary("intuition.library", LIBRARY_VERSION) )  )
  290.     {
  291.         return( "Unable to open the Intuition Library" );
  292.     }
  293.  
  294.     /*   Graphics   */
  295.     if(  ! ( GfxBase = (struct GfxBase *)
  296.         OpenLibrary("graphics.library", LIBRARY_VERSION) )  )
  297.     {
  298.         return( "Unable to open the Graphics Library" );
  299.     }
  300.  
  301.     return( NULL );
  302. }
  303.  
  304.  
  305.  
  306. /* This will be called prior to exiting.  Note that this will be
  307.  * called no matter what the reason for exiting is (normal completion
  308.  * or termination due to error).
  309.  */
  310.  
  311. void client_cleanup( void )
  312. {
  313.     if( IntuitionBase )
  314.         CloseLibrary( (struct Library *)IntuitionBase );
  315.  
  316.     if( GfxBase )
  317.         CloseLibrary( (struct Library *)GfxBase );
  318. }
  319.  
  320.